import os
import click

from flask import Flask
from pedestrian.settings import config
from pedestrian.blueprints.auth import auth_bp
from pedestrian.blueprints.console import console_bp
from pedestrian.blueprints.site import site_bp
from pedestrian.extensions import db


def create_app(config_name=None):
    if config_name is None:
        config_name = os.getenv('FLASK_CONFIG', 'development')

    app = Flask('pedestrian')
    app.config.from_object(config[config_name])

    register_blueprints(app)
    register_commands(app)
    register_extensions(app)

    return app


def regsiter_logging(app):
    pass


def register_extensions(app):
    pass


def register_blueprints(app):
    app.register_blueprint(site_bp)
    app.register_blueprint(auth_bp, subdomain='auth')
    app.register_blueprint(console_bp, subdomain='console')
    pass


def register_commands(app):
    @app.cli.command()
    @click.option('--auth', default=50, help='Quantity of auth, default is 50')
    @click.option('--solutionSafety', default=100, help='Quantity of Safety, default is 100')
    @click.option('--solutionAllotment', default=100, help='Quantity of Allotment, default is 100')
    def forge(auth, solutionSafety, solutionAllotment):
        '''
        Generates the fake auth, the numbers of solutionSafety and solutionAllotment
        :param auth:
        :param solutionSafety:
        :param solutionAllotment:
        :return:
        '''
        from pedestrian.fakes import fake_auth, fake_solutionAllotment, fake_solutionSafety

        db.drop_all()
        db.create_all()

        click.echo('Generating the auth...')
        fake_auth(auth)

        click.echo('Generating the solutionSafeties...')
        fake_solutionSafety(solutionSafety)

        click.echo('Generating the solutionAllotment')
        fake_solutionAllotment(solutionAllotment)

        click.echo('Done.')
